home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / 3DGPL 1.0 / CODE / GRAPHICS / GRP-BASE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-08  |  4.5 KB  |  118 lines  |  [TEXT/MACA]

  1. /** 3DGPL *************************************************\
  2.  * (8bit deep bitmap)                                     *
  3.  * 2D graphics and 2D clipping.                           *
  4.  *                                                        *
  5.  *  Defines:                                              *
  6.  *   G_init_graphics         initializing graphics;       *
  7.  *   G_clear                 clearing the bitmap;         *
  8.  *                                                        *
  9.  *   G_dot                   dot into the bitmap;         *
  10.  *   G_line                  line into a bitmap;          *
  11.  *                                                        * 
  12.  *  (6/1995) By Sergei Savhenko. (savs@cs.mcgill.ca).     *
  13.  *  Copyright (c) 1995 Sergei Savchenko.                  *
  14.  *  THIS SOURCE CODE CAN'T BE USED FOR COMERCIAL PURPOSES *
  15.  *  WITHOUT AUTHORISATION                                 *
  16. \**********************************************************/
  17.  
  18. #include <stdlib.h>                         /* malloc */
  19.  
  20. #ifdef __MWERKS__
  21. #include "hardware.h"           /* hardware specific stuff */
  22. #include "clipper.h"             /* 2D clipping routines */
  23. #include "graphics.h"           /* graphics functions */
  24. #else
  25. #include "../hardware/hardware.h"           /* hardware specific stuff */
  26. #include "../clipper/clipper.h"             /* 2D clipping routines */
  27. #include "../graphics/graphics.h"           /* graphics functions */
  28. #endif
  29.  
  30. unsigned char *G_buffer;                    /* the bitmap's bits */
  31.  
  32. /**********************************************************\
  33.  *  Allocating space for the colourmap.                   *
  34. \**********************************************************/
  35.  
  36. unsigned char *G_init_graphics(void)
  37. {
  38.  return(G_buffer=(unsigned char*)malloc(HW_COLOURMAP_SIZE_CHAR));
  39. }
  40.    
  41. /**********************************************************\
  42.  *  Clearing the bitmap with the specified colour.        *
  43. \**********************************************************/
  44.  
  45. void G_clear(void)
  46. {
  47.  HW_set_int((int*)G_buffer,HW_COLOURMAP_SIZE_INT,0x0);
  48. }
  49.  
  50. /**********************************************************\
  51.  *  Rendering a dot.                                      *
  52. \**********************************************************/
  53.  
  54. void G_dot(int *vertex,unsigned char colour)
  55. {
  56.  if( (vertex[0]>=0)&&(vertex[0]<HW_SCREEN_X_SIZE) &&
  57.      (vertex[1]>=0)&&(vertex[1]<HW_SCREEN_Y_SIZE) )
  58.  {
  59.   G_buffer[vertex[1]*HW_SCREEN_X_SIZE+vertex[0]]=colour;
  60.  }
  61. }
  62.  
  63. /**********************************************************\
  64.  *  Rendering a line.                                     *
  65. \**********************************************************/
  66.  
  67. void G_line(int *vertex1,int *vertex2,unsigned char colour)
  68. {
  69.  register int inc_ah,inc_al;                
  70.  register int i;                            
  71.  register unsigned char *adr=G_buffer;
  72.  int *v1,*v2;
  73.  int dx,dy,long_d,short_d;
  74.  int d,add_dh,add_dl;
  75.  int inc_xh,inc_yh,inc_xl,inc_yl;
  76.  
  77.  v1=vertex1;
  78.  v2=vertex2;
  79.  
  80.  if(C_line_x_clipping(&v1,&v2,2))           /* horizontal clipping */
  81.  {
  82.   if(C_line_y_clipping(&v1,&v2,2))          /* vertical clipping */
  83.   {
  84.    dx=v2[0]-v1[0]; dy=v2[1]-v1[1];          /* ranges */
  85.  
  86.    if(dx<0){dx=-dx; inc_xh=-1; inc_xl=-1;}  /* making sure dx and dy >0 */
  87.    else    {        inc_xh=1;  inc_xl=1; }  /* adjusting increments */
  88.  
  89.    if(dy<0){dy=-dy;inc_yh=-HW_SCREEN_X_SIZE;
  90.                    inc_yl=-HW_SCREEN_X_SIZE;
  91.            }                                /* to get to the neighboring */
  92.    else    {       inc_yh= HW_SCREEN_X_SIZE;/* point along Y have to add */
  93.                    inc_yl= HW_SCREEN_X_SIZE;/* or subtract this */
  94.            }                               
  95.    if(dx>dy){long_d=dx;short_d=dy;inc_yl=0;}/* long range,&making sure either */
  96.    else     {long_d=dy;short_d=dx;inc_xl=0;}/* x or y is changed in L case */
  97.  
  98.    inc_ah=inc_xh+inc_yh;
  99.    inc_al=inc_xl+inc_yl;                    /* increments for point address */
  100.    adr+=v1[1]*HW_SCREEN_X_SIZE+v1[0];       /* address of first point */
  101.  
  102.    d=2*short_d-long_d;                      /* initial value of d */
  103.    add_dl=2*short_d;                        /* d adjustment for H case */
  104.    add_dh=2*short_d-2*long_d;               /* d adjustment for L case */
  105.  
  106.    for(i=0;i<=long_d;i++)                   /* for all points in longer range */
  107.    {
  108.     *adr=colour;                            /* rendering */
  109.  
  110.     if(d>=0){adr+=inc_ah; d+=add_dh;}       /* previous point was H type */
  111.     else    {adr+=inc_al; d+=add_dl;}       /* previous point was L type */
  112.    }
  113.   }
  114.  }
  115. }
  116.  
  117. /**********************************************************/
  118.